package com.achun.ximalayamvvm.vm

import android.os.Build
import android.os.Handler
import androidx.annotation.RequiresApi
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.achun.ximalayamvvm.model.ServiceApi
import com.achun.ximalayamvvm.model.SubSQdataDAO
import com.achun.ximalayamvvm.utils.LogUtil
import com.achun.ximalayamvvm.utils.ToastUtil
import com.achun.ximalayamvvm.view.custom.SingleLiveEvent
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack
import com.ximalaya.ting.android.opensdk.model.album.Album
import com.ximalaya.ting.android.opensdk.model.track.Track
import com.ximalaya.ting.android.opensdk.model.track.TrackList
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.ObservableEmitter
import io.reactivex.rxjava3.core.ObservableOnSubscribe
import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.schedulers.Schedulers

@RequiresApi(Build.VERSION_CODES.P)
class RecommendDetailViewModel private constructor() : ViewModel(),
    SubSQdataDAO.OnSQLDaoChangeListenter {
    private var trackList = SingleLiveEvent<List<Track>>()
    var firstTrackList = SingleLiveEvent<List<Track>>()
    private var album = SingleLiveEvent<Album>()
    var isSetAlbum = false
    private var subAlbums=MutableLiveData<List<Album>>()
    private var loadsubMap=HashMap<Long,Album>()
    private var isSubscribed=SingleLiveEvent<Boolean>()
    lateinit var subSqlDao: SubSQdataDAO

    init {
        subSqlDao = SubSQdataDAO.getSUBDAO()!!
        subSqlDao.setOnSQLDaoChangeListenter(this)
    }

    companion object {
        val instance: RecommendDetailViewModel by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            RecommendDetailViewModel()
        }
    }
    fun getSubAlbums(): MutableLiveData<List<Album>> {
        return subAlbums
    }
    fun getIsSubsc(): SingleLiveEvent<Boolean> {
        return isSubscribed
    }
    fun setAlbums(album: Album) {
        this.album.value = album
        isSetAlbum = true
    }

    fun gettrackList(): SingleLiveEvent<List<Track>> {
        return trackList
    }

    fun getalbum(): SingleLiveEvent<Album> {
        return album
    }

    fun firstLoad() {
        ServiceApi.instance.getAlbumList(album.value?.id.toString(),
            object : IDataCallBack<TrackList> {
                override fun onSuccess(p0: TrackList?) {
                    LogUtil.e(this, "加载数据--》")
                    firstTrackList.value = p0?.tracks
                }

                override fun onError(p0: Int, p1: String?) {
                    LogUtil.e(this, "错误信息--》$p1")
                }

            })
    }

    fun loadData() {
        ServiceApi.instance.getAlbumList(album.value?.id.toString(),
            object : IDataCallBack<TrackList> {
                override fun onSuccess(p0: TrackList?) {
                    LogUtil.e(this, "加载数据--》")
                    trackList.value = p0?.tracks
                }

                override fun onError(p0: Int, p1: String?) {
                    LogUtil.e(this, "错误信息--》$p1")
                }

            })
    }

    override fun onCleared() {
        super.onCleared()
        LogUtil.e(this, "清除销毁")
    }

    //订阅类方法
    //读取订阅列表
    fun loadSubsced() {
        LogUtil.e(this,"读取订阅列表")
           subSqlDao.getAlbumList()
    }
    //是否订阅过
    fun isSubscribed(album: Album){
         isSubscribed.value = loadsubMap.containsKey(album.id)

    }


    //订阅
    fun addSub(album: Album) {
            if (subAlbums.value!!.size>100){
                ToastUtil.showToast("订阅数量已超标")
                return
            }
                subSqlDao.addAlbum(album)

    }

    //取消订阅
    fun deleteSub(album: Album) {

            subSqlDao.deleteAlbum(album)

    }

    //订阅事件回调
    override fun isAddSuccess(isSuccess: Boolean) {
        LogUtil.e(this,"添加订阅---$isSuccess")
        isSubscribed.value=isSuccess
       Handler().postDelayed({
           loadSubsced()
       },1000)
//
    }

    override fun isDeleteSuccess(isSuccess: Boolean) {
        isSubscribed.value=!isSuccess
        Handler().postDelayed({
            loadSubsced()
        },1000)
        LogUtil.e(this,"删除订阅---$isSuccess")
    }

    override fun getAlbumList(albums: List<Album>) {
        subAlbums.value=albums
        LogUtil.e(this,"订阅列表---${albums.toString()}")
        loadsubMap.clear()
        for (album in albums) {
            loadsubMap.put(album.id,album)
        }

    }
}